package org.geometerplus.zlibrary.core.filesystem;

import com.iwanvi.common.GlobalConstants;

import org.geometerplus.fbreader.filetype.FileType17KTxtNovel;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author enjoy
 */
public final class ZL17KPlainTxtFile extends ZLFile {

    public static final String ExtentionName = "17kTN";
    public static final String DirectoryExtentionName = ".in";

    private String myBookId;
    private String myBookName;

    private String myChapterId;

    private String mChapterName;

    public static ZL17KPlainTxtFile createByUri(String uri) {
        if (uri.startsWith(FileType17KTxtNovel.FileMode)) {
            String prefix = FileType17KTxtNovel.FileMode + GlobalConstants.BOOK_PATH + "/";
            String sub = uri.substring(prefix.length());
            String bookId = sub.substring(0, sub.indexOf("/"));
            String chapterId = sub.substring(sub.indexOf("/") + 1);
            return new ZL17KPlainTxtFile(bookId, "", chapterId, "");
        }
        return null;
    }

    @Override
    public String getShortName() {
        return ExtentionName;
    }

    public ZL17KPlainTxtFile(String bookId, String myBookName, String myChapterId, String myChapterName) {
        setBookId(bookId);
        setExtension(ExtentionName);
        setBookName(myBookName);
        setChapterName(myChapterName);
        setChapterId(myChapterId);

    }

    @Override
    public boolean exists() {
        File f = new File(getPath());
        return f.isFile() && f.exists();
    }

    @Override
    public InputStream getInputStream() throws IOException {

        return null;
    }

    @Override
    public String getLongName() {
//        return getPath() + "." + ExtentionName;
        // 使用上面注释掉的，书城图书试读时会生成两个File_id，所以改成下面这句 by huangyx 2016.4.5
        return GlobalConstants.BOOK_PATH + "/" + myBookId + "." + ExtentionName;
    }

    @Override
    public ZLFile getParent() {
        return null;
    }

    @Override
    public String getPath() {
        return GlobalConstants.BOOK_PATH + "/" + myBookId + "/" + (myChapterId == null ? "" : myChapterId);
    }

    @Override
    public ZLPhysicalFile getPhysicalFile() {
        return null;
    }

    @Override
    public boolean isDirectory() {
        return false;
    }

    @Override
    public long size() {
        return 0;
    }

    public void setBookId(String myBookId) {
        this.myBookId = myBookId;
    }

    @Override
    public String getBookId() {
        return myBookId;
    }

    @Override
    public String getBookName() {
        return myBookName;
    }

    public void setBookName(String myBookName) {
        this.myBookName = myBookName;
    }

    public void setChapterId(String chapterId) {
        myChapterId = chapterId;
    }

    @Override
    public String getChapterId() {
        return myChapterId;
    }

    @Override
    public String getChapterName() {
        return mChapterName;
    }

    public void setChapterName(String name) {
        mChapterName = name;
    }

    @Override
    public String getFileMode() {
        return FileType17KTxtNovel.FileMode;
    }
}
